package jia.tools;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.DocumentException;

import java.io.File;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class TavernNameGenerator {
    private static final Logger log = Logger.getLogger(TavernNameGenerator.class);
    private static File fileToBeParsed = new File("src/com/gryffonx/xml/tavern.xml");
    private static SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;
    
    public static String generateName(int howMany) {
        StringBuffer retVal = new StringBuffer();
        if (howMany <= 0) {
            howMany = 1;
        }
        
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        //determine the pattern that will be used.
        int whatPattern = determinePattern();
        //based on the pattern selected, generate the name
        for (int i = 0; i < howMany; i++) {
            StringBuffer adjVal = new StringBuffer();
            
            if (whatPattern == 1){
                adjVal.append(generateGroup1());
            }else if (whatPattern == 2){
                adjVal.append(generateGroup2());
            }else if (whatPattern == 3){
                adjVal.append(generateGroup3());
            }else if (whatPattern == 4){
                adjVal.append(generateGroup4());
            }else if (whatPattern == 5){
                adjVal.append(generateGroup5());
            }else if (whatPattern == 6){
                adjVal.append(generateGroup6());
            }else if (whatPattern == 7){
                adjVal.append(generateGroup7());
            }
            
            //assign the retVal to the name generated
            retVal.append(adjVal);
            //  retVal.append("\n");
            
        }
        
        //return the retVal
        return retVal.toString();
        
    }
    
    /**
     *
     *The [@GROUP1] [@GROUP2]<br>
     *The [@GROUP1] [@GROUP2] [@GROUP4]<br>
     *The House of [@GROUP3]<br>
     *The [@GROUP1] House of [@GROUP3]<br>
     *The [@GROUP2]'s [@GROUP3]<br>
     *The [@GROUP2]<br>
     *[@GROUP3] and [@GROUP3]<br>
     * @return
     */
    public static int determinePattern(){
        return StaticDiceRoller.rolldSomething(7,1);
    }
    
    private static String generateGroup1(){
        StringBuffer retVal = new StringBuffer();
        
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append(" ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        
        return retVal.toString();
    }
    private static String generateGroup2(){
        StringBuffer retVal = new StringBuffer();
        
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append(" ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
            retVal.append(" ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 4)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        return retVal.toString();
    }
    private static String generateGroup3(){
        StringBuffer retVal = new StringBuffer();
        
        try {
            retVal.append("The House of ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        return retVal.toString();
    }
    private static String generateGroup4(){
        StringBuffer retVal = new StringBuffer();
        
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append(" House of ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        
        return retVal.toString();
    }
    private static String generateGroup5(){
        StringBuffer retVal = new StringBuffer();
        
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
            retVal.append("'s ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        return retVal.toString();
    }
    private static String generateGroup6(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append("The ");
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
        
        return retVal.toString();
    }
    private static String generateGroup7(){
        StringBuffer retVal = new StringBuffer();
        
        retVal.append("The ");
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
        retVal.append(" and ");
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 3)));
        
        return retVal.toString();
    }
}
